package com.corner.presenter.imp;

import android.net.Uri;

import com.corner.R;
import com.corner.app.App;
import com.corner.model.cornerservice.domain.Info;
import com.corner.model.cornerservice.domain.Location;
import com.corner.model.cornerservice.helper.InfoHelper;
import com.corner.model.cornerservice.helper.LocationHelper;
import com.corner.model.cornerservice.service.exception.BaseException;
import com.corner.presenter.IChatCommentPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.view.IChatCommentView;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Administrator on 2015/7/14，13.10.
 * 描述：
 */
public class ChatCommentPresenter extends BasePresenter<IChatCommentView> implements IChatCommentPresenter {


    private Long infoId;

    @Override
    public void requestInfo(final Long infoId) {
        this.infoId = infoId;
        InfoHelper.getInfo(this.infoId).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Info>() {
            @Override
            public void call(Info info) {
                if (view != null && info.getObjectId().equals(infoId)) {
                    switch (info.getType()) {
                        case Info.T:
                            if (view != null) {
                                view.setContentText(info.getText());
                                view.setContentImgVisible(false);
                            }
                            break;
                        case Info.A:
                            if (view != null) {
                                view.setContentText(App.application.getString(R.string.chat_comment_audio));
                                view.setContentImgVisible(false);
                            }
                            break;
                        case Info.P:
                            if (view != null) {
                                view.setContentText("");
                                view.setImageUri(Uri.parse(info.getImageUrl()));
                                view.setContentImgVisible(true);
                            }
                            break;
                        case Info.TP:
                            if (view != null) {
                                view.setContentText(info.getText());
                                view.setImageUri(Uri.parse(info.getImageUrl()));
                                view.setContentImgVisible(true);
                            }
                            break;
                        case Info.AP:
                            if (view != null) {
                                view.setContentText(App.application.getString(R.string.chat_comment_pic_audio));
                                view.setImageUri(Uri.parse(info.getImageUrl()));
                                view.setContentImgVisible(true);
                            }
                            break;
                    }
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                if (view!=null&&throwable instanceof BaseException && ((BaseException)throwable).getCode().equals(BaseException.ObjectNotFound)){
                    view.setContentText("该内容已被删除");
                    view.setIsInfoDelete(true);
                }
            }
        });
    }

    @Override
    public void requestLocation(Long locationId) {
        LocationHelper.getLocation(locationId).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Location>() {
            @Override
            public void call(Location location) {
                if (view != null) view.setContentText(location.getLocationName());
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {

            }
        });
    }
}